package com.gthncz.xxhelper.adapters;

import android.content.Context;
import android.text.Layout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.gthncz.xxhelper.R;
import com.gthncz.xxhelper.greendao.Subject;

import java.util.List;

/**
 * Created by GT on 2019/11/11.
 */

public class SubjectListAdapter extends BaseAdapter {
    private Context context;
    private List<Subject> data;

    public SubjectListAdapter(Context context, List<Subject> data) {
        this.context = context;
        this.data = data;
    }

    public void setData(List<Subject> data){
        if (this.data != data){
            this.data.clear();
            this.data.addAll(data);
        }
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Subject subject = data.get(position);
        ViewHolder viewHolder = null;
        if (convertView == null){
            LayoutInflater inflater = LayoutInflater.from(context);
            convertView = inflater.inflate(R.layout.layout_item, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else viewHolder = (ViewHolder) convertView.getTag();
        viewHolder.setSubject(subject);
        return convertView;
    }

    private class ViewHolder {
        private View itemView;
        private TextView tv_question;
        private TextView tv_answer;

        public ViewHolder(View itemView) {
            this.itemView = itemView;
            tv_question = itemView.findViewById(R.id.textView_item_question);
            tv_answer = itemView.findViewById(R.id.textView_item_answer);
        }

        public void setSubject(Subject subject){
            tv_answer.setText(subject.getAnswer());
            tv_question.setText(subject.getQuestion());
            // Log.i("Adapter","set : " + subject.toString());
        }
    }
}
